<?php
/**
 * Created by Daniel X -- PhpStorm.  2020/7/24 15:30
 */
namespace App\Services;

use App\Http\Controllers\Traits\TransFormer;
use App\Models\Event;
use App\Models\Iss;
use Illuminate\Support\Facades\DB;

class ScoreService
{
    use TransFormer;

    protected $iss;
    protected $injuryService;
    public function __construct(Iss $iss,InjuryService $injuryService)
    {
        $this->iss = $iss;
        $this->injuryService = $injuryService;
    }

    public function saveIssData($data)
    {
        $where = ['patient_id'=>$data['patient_id']];
        $this->injuryService->checkPatientOK($where);
        $res = $this->iss->where($where)->get();
        if($res->isEmpty()){
            $data['status'] = 2;
            $iss_id = $this->iss->insertGetId($data);
            if(!$iss_id){
                $this->transFormerE(1003003, '添加失败');
            }
        }else{
            $patient_id = $data['patient_id'];
            unset($data['patient_id']);
            $data['status'] = 2;
            $this->iss->where(['patient_id'=>$patient_id])->update($data);
            $iss_id = $res[0]->id;
        }
        return ['iss_id'=>$iss_id];
    }

    public function getIssData($data)
    {
        $where = ['patient_id'=>$data['patient_id']];
        $this->injuryService->checkPatientOK($where);
        $res = $this->iss->where($where)->get();
        if($res->isEmpty()){
            $this->transFormerE(1003003, '获取失败');
        }
        $arr = [
            'patient_id' => $res[0]->patient_id,
            'status' => $res[0]->status,
            'iss_count' => $res[0]->iss_count,
            'ais_count' => $res[0]->ais_count
        ];
        return $arr;
    }
}